<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="批号" prop="purchaseProductBatchNo">
        <el-input v-model="dataForm.purchaseProductBatchNo" placeholder="批号"></el-input>
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input v-model="dataForm.productId" placeholder="产品ID"></el-input>
      </el-form-item>
      <el-form-item label="产品名称" prop="productNo">
        <el-input v-model="dataForm.productNo" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="入库ID" prop="productPutInStorageId">
        <el-input v-model="dataForm.productPutInStorageId" placeholder="入库ID"></el-input>
      </el-form-item>
      <el-form-item label="入库数量" prop="putInStorageNumber">
        <el-input v-model="dataForm.putInStorageNumber" placeholder="入库数量"></el-input>
      </el-form-item>
      <el-form-item label="采购单价" prop="purchaseUnitPrice">
        <el-input v-model="dataForm.purchaseUnitPrice" placeholder="采购单价"></el-input>
      </el-form-item>
      <el-form-item label="出库数量" prop="leaveStorageNumber">
        <el-input v-model="dataForm.leaveStorageNumber" placeholder="出库数量"></el-input>
      </el-form-item>
      <el-form-item label="出库完成时间" prop="leaveFinishTime">
        <el-input v-model="dataForm.leaveFinishTime" placeholder="出库完成时间"></el-input>
      </el-form-item>
      <el-form-item label="仓库" prop="location">
        <el-input v-model="dataForm.location" placeholder="仓库"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="创建者" prop="createUser">
        <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="更新者" prop="updateUser">
        <el-input v-model="dataForm.updateUser" placeholder="更新者"></el-input>
      </el-form-item>
      <el-form-item label="采购合同ID" prop="contractPurchaseId">
        <el-input v-model="dataForm.contractPurchaseId" placeholder="采购合同ID"></el-input>
      </el-form-item>
      <el-form-item label="采购订单ID" prop="purchaseProductId">
        <el-input v-model="dataForm.purchaseProductId" placeholder="采购订单ID"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        purchaseProductBatchNo: '',
        productId: '',
        productNo: '',
        productPutInStorageId: '',
        putInStorageNumber: '',
        purchaseUnitPrice: '',
        leaveStorageNumber: '',
        leaveFinishTime: '',
        location: '',
        status: '',
        createTime: '',
        createUser: '',
        updateTime: '',
        updateUser: '',
        contractPurchaseId: '',
        purchaseProductId: ''
      },
      dataRule: {
        purchaseProductBatchNo: [
          {required: true, message: '批号不能为空', trigger: 'blur'}
        ],
        productId: [
          {required: true, message: '产品ID不能为空', trigger: 'blur'}
        ],
        productNo: [
          {required: true, message: '产品名称不能为空', trigger: 'blur'}
        ],
        productPutInStorageId: [
          {required: true, message: '入库ID不能为空', trigger: 'blur'}
        ],
        putInStorageNumber: [
          {required: true, message: '入库数量不能为空', trigger: 'blur'}
        ],
        purchaseUnitPrice: [
          {required: true, message: '采购单价不能为空', trigger: 'blur'}
        ],
        leaveStorageNumber: [
          {required: true, message: '出库数量不能为空', trigger: 'blur'}
        ],
        leaveFinishTime: [
          {required: true, message: '出库完成时间不能为空', trigger: 'blur'}
        ],
        location: [
          {required: true, message: '仓库不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],
        createUser: [
          {required: true, message: '创建者不能为空', trigger: 'blur'}
        ],
        updateTime: [
          {required: true, message: '更新时间不能为空', trigger: 'blur'}
        ],
        updateUser: [
          {required: true, message: '更新者不能为空', trigger: 'blur'}
        ],
        contractPurchaseId: [
          {required: true, message: '采购合同ID不能为空', trigger: 'blur'}
        ],
        purchaseProductId: [
          {required: true, message: '采购订单ID不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/product/productbatchnoqueue/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.purchaseProductBatchNo = data.productBatchNoQueue.purchaseProductBatchNo
              this.dataForm.productId = data.productBatchNoQueue.productId
              this.dataForm.productNo = data.productBatchNoQueue.productNo
              this.dataForm.productPutInStorageId = data.productBatchNoQueue.productPutInStorageId
              this.dataForm.putInStorageNumber = data.productBatchNoQueue.putInStorageNumber
              this.dataForm.purchaseUnitPrice = data.productBatchNoQueue.purchaseUnitPrice
              this.dataForm.leaveStorageNumber = data.productBatchNoQueue.leaveStorageNumber
              this.dataForm.leaveFinishTime = data.productBatchNoQueue.leaveFinishTime
              this.dataForm.location = data.productBatchNoQueue.location
              this.dataForm.status = data.productBatchNoQueue.status
              this.dataForm.createTime = data.productBatchNoQueue.createTime
              this.dataForm.createUser = data.productBatchNoQueue.createUser
              this.dataForm.updateTime = data.productBatchNoQueue.updateTime
              this.dataForm.updateUser = data.productBatchNoQueue.updateUser
              this.dataForm.contractPurchaseId = data.productBatchNoQueue.contractPurchaseId
              this.dataForm.purchaseProductId = data.productBatchNoQueue.purchaseProductId
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/productbatchnoqueue/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'purchaseProductBatchNo': this.dataForm.purchaseProductBatchNo,
              'productId': this.dataForm.productId,
              'productNo': this.dataForm.productNo,
              'productPutInStorageId': this.dataForm.productPutInStorageId,
              'putInStorageNumber': this.dataForm.putInStorageNumber,
              'purchaseUnitPrice': this.dataForm.purchaseUnitPrice,
              'leaveStorageNumber': this.dataForm.leaveStorageNumber,
              'leaveFinishTime': this.dataForm.leaveFinishTime,
              'location': this.dataForm.location,
              'status': this.dataForm.status,
              'createTime': this.dataForm.createTime,
              'createUser': this.dataForm.createUser,
              'updateTime': this.dataForm.updateTime,
              'updateUser': this.dataForm.updateUser,
              'contractPurchaseId': this.dataForm.contractPurchaseId,
              'purchaseProductId': this.dataForm.purchaseProductId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
